/**
 * Created by jimmy on 2017/6/10.
 */

var prodService = require("../service/ProdService");
var async = require("async");

exports.index = function (req,res,next) {
    var prodTypes = null;
    async.series({
        prodTypes:function (callbk) {
            prodService.getTypeList(callbk);
        },
        pdList:function (callbk) {
            prodService.getPdlist(callbk);
            // callbk(null,null);
        }
    },function (err,result) {
        console.log(result.pdList);
        if(err){
            console.log(err)
        }else {
            console.log(JSON.stringify(result.prodList));
            res.render("productlist",{title:"商品列表",home:"",prod:"on",prebuy:"",my:"",prodTypes:result.prodTypes,pdList:result.pdList});
        }
    });
}

exports.getProdList = function(req,res,next){
    var typeId = req.query.typeId;
    async.series({
        prodList:function (callbk) {
            prodService.getProdList(typeId,callbk);
        }
    },function (err,result) {
        if(err){
            console.log(err)
        }else {
            console.log(JSON.stringify(result.prodList));
            // res.render("productlist",{title:"商品列表",home:"",prod:"on",prebuy:"",my:"",prodList:result.prodList});
            res.status(200).end(JSON.stringify(result.prodList));
        }
    });
};

exports.getProdDetail = function (req,res,next) {
    var prodId = req.query.prodId;
    async.series({
        prod:function (callbk) {
            prodService.getProd(prodId,callbk);
        },
        prodpic:function (callbk) {
            prodService.getProdDetail(prodId,'dpic',callbk);
        },
        prodspec:function (callbk) {
            prodService.getProdDetail(prodId,'spec',callbk);
        },
        proddesc:function (callbk) {
            prodService.getProdDetail(prodId,'desc',callbk);
        }
    },function (err,result) {
        if(err){
            console.log(err)
        }else {
            console.log(JSON.stringify(result.prod));
            res.render("pddetail",{title:"商品详情",prod:result.prod,prodpic:result.prodpic,prodspec:result.prodspec,proddesc:result.proddesc})
        }
    })
}